import java.util.*;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution2 {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) {
            return pRootOfTree;
        }
        fun(pRootOfTree);
        while(pRootOfTree.left != null) {
            pRootOfTree = pRootOfTree.left;
        }
        return pRootOfTree;
    }

    public TreeNode pre = null;
    public void fun(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left != null) {
            fun(root.left);
        }
        root.left = pre;
        if(pre != null) {
            pre.right = root;
        }
        pre = root;
        fun(root.right);
    }
}
